package com.service.calculate.vehicle;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.service.bean.device.Vehicle;
import com.service.bean.other.VariableData;
import com.service.bean.signal.GpsSignal;
import com.service.cache.CacheManager;
import com.service.other.Logger;

/**
 * 出勤报表数据源计算
 * @author lj
 *
 */
public class DutyReportCal {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 车辆 
	 */
	private Vehicle vehicle;
	/**
	 * 当前定位信号
	 */
	private GpsSignal signal;
	
	public DutyReportCal(Vehicle vehicle, GpsSignal signal) {
		this.vehicle = vehicle;
		this.signal = signal;
	}
	
	/**
	 * 出勤计算
	 */
	public void attendanceCal() {
		if (null == vehicle || null == vehicle.duty) {
			Logger.debug(vehicle.device.deviceNo, "车辆无排班");
			// 没有车辆或车辆当前无排班
			return;
		}
		if (null != vehicle.getAreas() && 0 != vehicle.getAreas().size()) {
			if (null == signal.areaId) {
				// 车辆不在规划的区域中
				Logger.debug(vehicle.device.deviceNo, "车辆不在排班/规划的区域/路线中");
				return;
			}
		}
		if (0 == signal.accStatus) {
			// 车辆acc关闭
			Logger.debug(vehicle.device.deviceNo, "车辆当前acc关闭");
			return;
		}
		workStartCal();
		workEndCal();
		dutyCal();
	}
	
	/**
	 * 上班浮动时间段出勤计算
	 */
	private void workStartCal() {
		Logger.debug(vehicle.device.deviceNo, "-------------------------检查上班出勤情况-------------------------");
		// 最早上班时间
		Date early = null;
		// 最晚上班时间
		Date late = null;
		if (vehicle.duty.startFloatTime <= 0) {
			// 如果未设置浮动时间, 默认浮动时间为1分钟
			early = new Date(vehicle.duty.workStartTime.getTime() - 60000L);
			late = new Date(vehicle.duty.workStartTime.getTime() + 60000L);
		} else {
			early = new Date(vehicle.duty.workStartTime.getTime() - vehicle.duty.startFloatTime * 60 * 1000);
			late = new Date(vehicle.duty.workStartTime.getTime() + vehicle.duty.startFloatTime * 60 * 1000);
		}
		if (signal.gpsTime.after(early) && signal.gpsTime.before(late)) {
			// 在上班考勤时间段内
			VariableData variable = CacheManager.instance().getVariable(signal.gpsNo);
			if (null == variable.activeWorkStartTime) {
				// 只记录一次出勤
				variable.activeWorkStartTime = signal.gpsTime;
				Logger.debug(vehicle.device.deviceNo, "检测到第一次上班出勤, 将该时间点记录到缓存中: " + sdf.format(signal.gpsTime));
			} else {
				Logger.debug(vehicle.device.deviceNo, "在上班时间段内有工作记录,不重复记录,当前gps时间:" + sdf.format(signal.gpsTime) + ", 记录的时间: " + sdf.format(variable.activeWorkStartTime));
			}
		} else {
			Logger.debug(vehicle.device.deviceNo, "车辆当前不在上班时间段内,当前gps时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(signal.gpsTime));
		}
	}
	
	/**
	 * 下班浮动时间段出勤计算
	 */
	private void workEndCal() {
		Logger.debug(vehicle.device.deviceNo, "-------------------------检查下班出勤情况-------------------------");
		// 最早下班时间
		Date early = null;
		// 最晚下班时间
		Date late = null;
		if (vehicle.duty.endFloatTime <= 0) {
			// 如果未设置浮动时间, 默认浮动时间为1分钟
			early = new Date(vehicle.duty.workEndTime.getTime() - 60000L);
			late = new Date(vehicle.duty.workEndTime.getTime() + 60000L);
		} else {
			early = new Date(vehicle.duty.workEndTime.getTime() - vehicle.duty.endFloatTime * 60 * 1000);
			late = new Date(vehicle.duty.workEndTime.getTime() + vehicle.duty.endFloatTime * 60 * 1000);
		}
		if (signal.gpsTime.after(early) && signal.gpsTime.before(late)) {
			// 在上班考勤时间段内
			VariableData variable = CacheManager.instance().getVariable(signal.gpsNo);
			if (null == variable.activeWorkEndTime) {
				// 只记录一次出勤
				variable.activeWorkEndTime = signal.gpsTime;
				Logger.debug(vehicle.device.deviceNo, "检测到第一次下班出勤, 将该时间点记录到缓存中: " + sdf.format(signal.gpsTime));
			} else {
				Logger.debug(vehicle.device.deviceNo, "在下班时间段内有工作记录,不重复记录,当前gps时间:" + sdf.format(signal.gpsTime) + ", 记录的时间: " + sdf.format(variable.activeWorkEndTime));
			}
		} else {
			Logger.debug(vehicle.device.deviceNo, "车辆当前不在下班时间段内,当前gps时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(signal.gpsTime));
		}
	}
	
	/**
	 * 当前班次的出勤情况
	 */
	private void dutyCal() {
		// 最早上班时间
		Date early = null;
		// 最晚下班时间
		Date late = null;
		if (vehicle.duty.startFloatTime <= 0)
			early = new Date(vehicle.duty.workStartTime.getTime() - 60000L);
		else
			early = new Date(vehicle.duty.workStartTime.getTime() - vehicle.duty.startFloatTime * 60 * 1000);
		if (vehicle.duty.endFloatTime <= 0)
			late = new Date(vehicle.duty.workEndTime.getTime() - 60000L);
		else 
			late = new Date(vehicle.duty.workEndTime.getTime() - vehicle.duty.endFloatTime * 60 * 1000);
		if (signal.gpsTime.after(early) && signal.gpsTime.before(late)) {
			VariableData variable = CacheManager.instance().getVariable(signal.gpsNo);
			// 出勤次数+1
			variable.workRecord++;
		}
	}
	
}
